package com.fourGirlsAndADude.mobileDRT;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import com.att.intern.webservice.Incident;
import com.att.intern.webservice.Webservice.TokenInvalidException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/*
 * GeolocSearchActivity
 * This class allows for search for incidents by providing the Geoloc of a property
 */
public class GeolocSearchActivity extends Activity {
	
	/*
	 * These are the fields for the incidents that get returned from the Geoloc search
	 */
	public String geoloc;
	public String buildingName;
	public String state;
	public String PM;
	public String address;
	public static Incident aIncid;

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_geoloc_search);
		
		final String user = this.getIntent().getStringExtra("USER_NAME");
		final String authorizationType = this.getIntent().getStringExtra("AUTHORIZATION_TYPE");
		final String sessionToken = this.getIntent().getStringExtra("TOKEN_ID");
		//picture
		final String picture = this.getIntent().getStringExtra("PictureFile");
		
		TextView signed = (TextView) findViewById(R.id.signed);
        signed.setText("Signed in as "+ user +": "+ authorizationType);
        final EditText search = (EditText) findViewById(R.id.searchGLC);
        final Context context = getApplicationContext();
        final Toast blankError = Toast.makeText(context, "Please enter GLC", Toast.LENGTH_SHORT);
        final Toast invalidError = Toast.makeText(context, "Please enter a valid glc!", Toast.LENGTH_SHORT);
        //Toast exception = Toast.makeText(context, "Exception!", Toast.LENGTH_LONG);
		final Toast timeOut = Toast.makeText(context, "You will be logged out!", Toast.LENGTH_SHORT);

        
        Button searchBtn = (Button) findViewById(R.id.searchBtn);
        searchBtn.setOnClickListener(new OnClickListener(){

        	//@Override
			public void onClick(View v) {


				//final Context con = getApplicationContext();
		    	//final Webservice test = new Webservice(con);

				geoloc = search.getText().toString(); //getting user input, converting number to string

				Hashtable<Integer,String> glcInfo = LoginActivity.webService.getGLCInfo();


				if(geoloc.equals("")){
					blankError.show();
				}
				else if((geoloc.length() < 4) || (geoloc.length() > 12)){
					blankError.show();
				}

				else{
					try {

						//if (geolocint) does not exist in the hashtable, start GLCResultNoActivity
							//check if this glc exists as a key in the hashtable
						//else

						Enumeration<Integer> glckeys = glcInfo.keys();
						boolean found=false;
						while (glckeys.hasMoreElements()){
							Integer element = (Integer)glckeys.nextElement();
							String elem= Integer.toString(element);
							if(geoloc.equals(elem)){
								found=true;
								break;
							}
						}
						if (found){ //we know that this is a valid GLC
							int geolocint = Integer.parseInt(geoloc); //converting string to an int
							String glcvalue = glcInfo.get(new Integer(geolocint));
							String [] glcvalues = glcvalue.split("~");
							
							state = glcvalues[0];
							PM = glcvalues[1];
							buildingName = glcvalues[2];
							address = glcvalues[3];
							
							ArrayList<Incident> locs = LoginActivity.webService.geolocSearch(sessionToken, geolocint); //returns ONLY open incidents
							boolean open=false;
	
							if(locs.size()!=0){ //could have 0 open incidents, either closed or just don't have any incidents for this particular glc/geoloc
								for(Incident i: locs){  //removable....
									if(i.getIncidentStatus().equalsIgnoreCase("Open"))
										open=true;
										Intent k = new Intent();
										k.setClassName("com.fourGirlsAndADude.mobileDRT", "com.fourGirlsAndADude.mobileDRT.GLCResultOpenActivity");
										k.putExtra("GLC_RESULTS",locs);
										k.putExtra("USERNAME", user);
										k.putExtra("SessionToken", sessionToken);
					        			k.putExtra("AUTHORIZATIONTYPE", authorizationType);
					        			//picture
					        			k.putExtra("PictureFile", picture);
					        			startActivity(k);
								}
							}
							//if(!open){
							else{
								//Log.w("TEST","Got to if");
								Intent i = new Intent();
			        			i.setClassName("com.fourGirlsAndADude.mobileDRT", "com.fourGirlsAndADude.mobileDRT.GLCResultNoActivity");
			        			i.putExtra("GLC_RESULTS",locs);
			        			//Added by Thaddeus so that I could implement the work around christopher potts provided
			        			//i.putExtra("HASH_TABLE_GLC_INFO" ,glcInfo );
			        			i.putExtra("GEOLOC_INT", geolocint);
			        			i.putExtra("USERNAME", user);
			        			i.putExtra("SessionToken", sessionToken);
			        			i.putExtra("AUTHORIZATIONTYPE", authorizationType);
			        			startActivity(i);
			        			finish();
							}
							
							
						}
						else{
							invalidError.show();
						}

					}//end try
					catch (TokenInvalidException e) {
						// TODO Auto-generated catch block
						Log.v("GeolocSearchActivity", "Has caught TokenInvalidException");
						e.printStackTrace();
						timeOut.show();
						Intent i = new Intent();
						i.setClassName("com.fourGirlsAndADude.mobileDRT", "com.fourGirlsAndADude.mobileDRT.LoginActivity");
	        			startActivity(i);
					}//end catch
				}//end else
			}//end onClick
        }); //end onClickListener
	}//end onCreate()

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.geoloc_search, menu);
		return true;
	}

}//end class
